﻿@model MaterialPurchase
@{
    ViewBag.Title = "采购订单维护";
    Layout = "~/Views/Shared/_LayoutFormDetail.cshtml";
}
@section Up{
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.State)
    @Html.HiddenFor(model => model.OrderDate)
    @Html.nbDropDownListFor(model => model.SupplierId, "Supplier").Required()
    @Html.nbDateFor(model => model.Date)
    @Html.nbTextBoxFor(model => model.TotalAmount).ReadOnly()
}
@section Down{
    @(Html.nbDetailFor<MaterialPurchaseItem>(s =>
    {
        s.ID("MaterialList");
        s.layoutW(10);
        s.layoutH(162);
        s.Columns(c =>
        {
            c.Bind(m => m.Id).IsKey();
            c.Bind(m => m.MaterialId).Hide();
            c.BindEnum(m => m.Category, "MaterialCategory").Width(50);
            c.Bind(m => m.Code).Width(60);
            c.Bind(m => m.Name).Width(100).Align(GridAlign.Left);
            c.BindControl(m => m.Price, GridControlType.Label).Width(30).Title("价格").Align(GridAlign.Right);
            c.BindEnum(m => m.Unit, "MaterialUnit").Title("单位").Width(30);
            c.BindControl(m => m.OrderCount).Title("订购量").Width(50).Align(GridAlign.Right);
            c.BindControl(m => m.Amount).Width(50).Title("金额").Align(GridAlign.Right);
            c.BindTemplate(50)
               .Template(@<a href="javascript:DeleteRow('${Id}')" class="imgbtn2"><span class="Delete">删除</span></a>);
        });
        s.Buttons(b =>
        {
            b.BindAddButton("添加品名").OnClick("SelectMaterial");
        });
    }))
}
<script type="text/javascript">
    function SelectMaterial(e) {
        OpenModalDialog('@Url.Action("LookUp", "Material", new { NeedProcess = false })',
        {
            width: 800,
            height: 500,
            type: 2,
            onclose:
            function (MaterialData) {
                if (MaterialData) {
                    MapJsonArray(MaterialData, "Id", "MaterialId");
                    MapJsonArray(MaterialData, "CurrentPrice", "Price");
                    $("#MaterialList").flexAddRows(MaterialData);
                }
            }
        });
    }

    $(function () {
        $("#MaterialList").delegate("#MaterialList input[name='OrderCount']", 'change', function (e) {
            var $this = $(this);
            var tr = $this.closest("tr");
            var inputPrice = tr.find("input[name='Price']");
            var inputAmount = tr.find("input[name='Amount']");
            inputAmount.val(parseFloat($this.val() * inputPrice.val()).toFixed(2));
            e.preventDefault();

            AutoSum();
        })
    });

    function AutoSum() {
        var data = $("#MaterialList").flexGetData();
        var TotalCost = 0;
        for (var i = 0; i < data.length; i++) {
            TotalCost += data[i].Amount * 1;
        }
        $('#TotalAmount').val(TotalCost.toFixed(2));
    }

    function DeleteRow(id) {
        $("#MaterialList").flexDeleteRowData(id);
        AutoSum();
    }
</script>
